<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class UsercardController extends HomebaseController {
	public function addusercard() {
		$Card=M('card');
		$card=$Card->select();
		$this->assign('card',$card);
		$this->display(":addusercard");
		$Usercard=M('usercard');
		$Member=M('member');
		$arr=$_POST;
		$arr['deadtime']=strtotime($arr['deadtime']);
		if($_POST['deadtime']&&$_POST['uid']&&$_POST['cardname']){
			$msg1=$Usercard->add($arr);
			$where['uid']=$_POST['uid'];
			if($_POST['name']){
				$data['name']=$_POST['name'];
			}
			if($_POST['tel']){
				$data['tel']=$_POST['tel'];
			}
			if($data){
				$msg2=$Member->where($where)->save($data);
			}
			if($msg1){
				$this->succ('showusercard');
			}
			else{
				$this->err('showusercard');
			}
		}
	}
	public function clear(){
		$_POST=null;
		session("usercardwhere['uid']",null);
		session("usercardwhere['name']",null);
		session("usercardwhere['tel']",null);
		$this->showmember();
	}
	public function showusercard(){
		$Usercard=M('usercard');
		$Member=M('member');
		//uid
		if($_POST['uid']){
			$where1['uid']=$_POST['uid'];
			session("usercardwhere['uid']",$where1['uid']);
		}else if(!$_POST['uid'] && session("usercardwhere['uid']")){
			$where1['uid']=session("usercardwhere['uid']");
		}
		//name
		if($_POST['name']){
			$where1['name']=$_POST['name'];
			session("usercardwhere['name']",$where1['name']);
		}else if(!$_POST['name'] && session("usercardwhere['name']")){
			$where1['name']=session("usercardwhere['name']");
		}
		//tel
		if($_POST['tel']){
			$where1['tel']=$_POST['tel'];
			session("usercardwhere['tel']",$where1['tel']);
		}else if(!$_POST['tel'] && session("usercardwhere['tel']")){
			$where1['tel']=session("usercardwhere['tel']");
		}
		if($_POST){
			$uid=$Member->where($where1)->getField('uid',true);
			if($uid){
				$where['uid']=array('in',$uid);
			}else{
				echo "<script>alert('查无此信息');history.go(-1);</script>";
				die();
			}
		}
		$count=$Usercard->where($where)->count();
		$page=$this->page($count,10);
		$usercard=$Usercard
		->where($where)
		->order('deadtime')
		->limit($page->firstRow , $page->listRows)
		->select();
		for($i=0;$i<count($usercard);$i++){
			$where['uid']=$usercard[$i]['uid'];
			$usercard[$i]['name']=$Member->where($where)->getField('name');
			$usercard[$i]['tel']=$Member->where($where)->getField('tel');
			$usercard[$i]['deadtime']=date('Y-m-d',$usercard[$i]['deadtime']);
		}
		$this->assign('usercard',$usercard)
		->assign('where',$where1)
		->assign("page", $page->show('Admin'));
		$this->display(':showUsercard');
	}
	//删除
	public function delusercard() {
		$where['ucid']=$_GET['ucid'];
		$Usercard=M('usercard');
		$msg=$Usercard->where($where)->delete();//where语句中的变量需要引号
		if($msg){
			$this->succ('showusercard');
		}
	}
	//编辑
	public function editusercard() {
			$Usercard=M('usercard');
			$where['ucid']=$_GET['ucid'];
			$msg=$Usercard->where($where)->find();
			$msg['deadtime']=date("Y-m-d",$msg['deadtime']);
			$this->assign('msg',$msg);
			$this->display(":editusercard");
			$arr=$_POST;
			if($_POST['deadtime']&&$_POST['uid']&&$_POST['cardname']){
				$arr['deadtime']=strtotime($arr['deadtime']);
				$msg=$Usercard->where($where)->save($arr);
				if($msg){
					$this->succ('showusercard');
				}
				else{
					$this->err('showusercard');
				}
			}
	}
	//操作成功
	public function succ($m){
		$C='Usercard';
		$this->assign('m',$m)->assign('C',$C);
		$this->display(':success');
	}
	//操作失败
	public function err($m){
		$C='Usercard';
		$this->assign('m',$m)->assign('C',$C);		
		$this->display(':err');
	}
}